package org.Image.management.ViewService.Game.CopyUtils;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.stage.StageStyle;
import lombok.extern.slf4j.Slf4j;
import org.Image.management.SpringDataService.Service.StorageIo.Pojo.Mapper.FileStorage;
import org.Image.management.ViewController.MainViewApp;
import org.Image.management.ViewService.Game.pojo.TableImage;
import org.Image.management.ViewService.utils.informationalTips;
import org.mapstruct.Mapper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.function.Consumer;

@Slf4j
@Mapper(componentModel = "spring")
public abstract class CopySaveImage {
//    public static final CopySaveImage INSTANCE = Mappers.getMapper(CopySaveImage.class);

    public List<TableImage> ListFileStorageToTableImage(List<FileStorage> fileStorages, Consumer<Map<String, Object>> UpdateSaveImage) {
        ArrayList<TableImage> objects = new ArrayList<>();
        fileStorages.forEach(e -> {
            objects.add(FileStorageToTableImage(e, UpdateSaveImage));
        });
        return objects;
    }


    /***
     *
     * @param fileStorage ->  数据库图片
     * @param UpdateSaveImage -> 更改图片
     *            Map key 为 ：
     *                   更改的类别 (TheCategory) ：
     *                          value 为 String：
     *                                      ReviseActionEvent -> 修改图片
     *                                      ShowActionEvent -> 显示图片
     *                                      DetActionEvent -> 删除图片
     *                                      ReviseDescriptionActionEvent -> 修改备注信息
     *                   当前FileStorage数据 (FileStorageData):
     *                          value 为：FileStorage 类
     *                   自定义数据 (Data) :
     *                          value 为: 自定义数据 请看各自实现
     * @return TableImage 类
     */
    public TableImage FileStorageToTableImage(FileStorage fileStorage, Consumer<Map<String, Object>> UpdateSaveImage) {
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(fileStorage.getPath());
        } catch (FileNotFoundException e) {
            log.info("文件不存在");
        }
        Image image = new Image(Objects.requireNonNull(fileInputStream));


        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("FileStorage", fileStorage);

        // 修改
        EventHandler<ActionEvent> reviseActionEvent = event -> {
            stringObjectHashMap.put("TheCategory", "ReviseActionEvent");

            // 获取修改的位置图片
            File showOpenDialogFile = MainViewApp.setResizable.getShowOpenDialogFile();
            if (showOpenDialogFile == null) {
                return;
            }
            stringObjectHashMap.put("Data", showOpenDialogFile);
            UpdateSaveImage.accept(stringObjectHashMap);
        };

        // 显示
        EventHandler<ActionEvent> ShowActionEvent = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stringObjectHashMap.put("TheCategory", "ShowActionEvent");
                stringObjectHashMap.put("Data", image);
                UpdateSaveImage.accept(stringObjectHashMap);
            }
        };

        //删除图片
        EventHandler<ActionEvent> detActionEvent = event -> {
            stringObjectHashMap.put("TheCategory", "DetActionEvent");
            stringObjectHashMap.put("Data", fileStorage.getUuid());
            UpdateSaveImage.accept(stringObjectHashMap);
        };
        // 修改备注信息
        EventHandler<MouseEvent> reviseDescriptionActionEvent = event -> informationalTips.textEntryDialogBox(
                StageStyle.UTILITY, "请输入名称", "请输入修改的备注信息", false, (ev, textInputDialog) -> {
                    String remark = textInputDialog.getEditor().getText();
                    stringObjectHashMap.put("TheCategory", "ReviseDescriptionActionEvent");
                    stringObjectHashMap.put("Data", remark);
                    UpdateSaveImage.accept(stringObjectHashMap);
                }
        );

        return new TableImage(image, reviseActionEvent, ShowActionEvent, detActionEvent,
                fileStorage.getDescription(), reviseDescriptionActionEvent);
    }


}
